using System;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Drawing;
using System.Drawing.Drawing2D;

namespace Camalon.Drawing.Shapes
{
	public enum PieType
	{
		Default,
		Arc,
		ClosedArc
	}


	[ToolboxItem(true)]
    [Designer("Camalon.Drawing.Design.PieDesigner, Camalon.Drawing.Design", typeof(IDesigner))]
    public class Pie : Shape
	{
//		public Pie(System.ComponentModel.IContainer container):this()
//		{
//			///
//			/// Required for Windows.Forms Class Composition Designer support
//			///
//			container.Add(this);
//
//		}

		public Pie(): base( new Rectangle( Point.Empty, new Size( 300,200))){}

		protected override GraphicsPath MakePath(RectangleF bounds)
		{
			GraphicsPath path = new GraphicsPath();

			float sweepAngle = _sweepAngle == 0f ? 0.1f: _sweepAngle;
			if( _pieType == PieType.Default)
			{
				path.AddPie( bounds.X, bounds.Y, bounds.Width, bounds.Height, _startAngle, sweepAngle);
			}
			else
			{
				path.AddArc( bounds.X, bounds.Y, bounds.Width, bounds.Height, _startAngle, sweepAngle);
				if( _pieType == PieType.ClosedArc) path.CloseAllFigures();
			}

			return path;
		}


		private int _startAngle= 0;

		[DefaultValue(0)]
		[Category("Layout")]
		public int StartAngle
		{
			get{ return _startAngle;}
			set
			{ 
				if( value == _startAngle) return;
				_startAngle = value;
				InvokeInternalChanged( true);
			}
		}

		private int _sweepAngle= 90;

		[DefaultValue(90)]
		[Category("Layout")]
		public int SweepAngle
		{
			get{ return _sweepAngle;}
			set
			{ 
				if( value == _sweepAngle) return;
				_sweepAngle = value;
				InvokeInternalChanged( true);
			}
		}

		private PieType _pieType = PieType.Default;
		[Category("Layout")]
		[DefaultValue(PieType.Default)]
		public PieType PieType
		{
			get{ return _pieType;}
			set
			{
				if( value == _pieType) return;

				_pieType = value;
				InvokeInternalChanged( true);
			}
		}

		private double GetAngle( PointF rotatingPoint, PointF anchorPoint )
		{
			//Recalculate radius
			//------------------------------------------------
			PointF pt = new PointF (rotatingPoint.X - anchorPoint.X , rotatingPoint.Y  - anchorPoint.Y  ); 
			double radian = Math.Atan2( pt.Y, pt.X );
			double angle = radian * (180.0/Math.PI);
			if( angle < 0.0 ) angle = 360.0 + angle;
			
			if( Convert.ToInt32( angle) == 360 ) angle = 0;

			return angle;
		}


		[Browsable( false)]
		[DesignerSerializationVisibility( DesignerSerializationVisibility.Hidden)]
		public override PointF[] ControlPoints
		{
			get
			{
				PointF[] points = new PointF[2];
				PointF[] pathPoints = base.BasePath.PathPoints;
				if( _pieType == PieType.Default) //points[0] = pathPoints[0];//Center
					points[0] = pathPoints[1];//Start
				else 
					points[0] = pathPoints[0];//Start

				points[1] = pathPoints[pathPoints.Length - 1];//End

				return points;

			}
			set
			{
				PointF anchor = RectangleUtil.CenterPoint( base.Bounds );
				_startAngle = Convert.ToInt32(GetAngle( value[0], anchor));
				_sweepAngle = Convert.ToInt32(GetAngle( value[1], anchor));

				if( _sweepAngle > _startAngle ) _sweepAngle -= _startAngle;
				else _sweepAngle = (360 - _startAngle)  + _sweepAngle;

				if( _sweepAngle == 360 ) _sweepAngle = 0;


				InvokeInternalChanged( true );
			}
		}


	}
}
